package com.sirdc.modules.sys.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysDeptRoomDao;
import com.sirdc.modules.sys.databean.SysDeptRoomDataBean;
import com.sirdc.modules.sys.databean.tree.DeptRoomDTO;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysDeptRoom;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysDeptFilter;
import com.sirdc.modules.sys.filter.SysDeptRoomFilter;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysDeptRoomService extends StringPKService<SysDeptRoom> {
    @Autowired
    private SysDeptRoomDao dao;
    @Autowired
    private SysDeptService deptService;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private SysLoginService loginService;
    @Override
    protected void beforeSave(SysDeptRoom obj) {
        obj.setSysId(tableService.updateMaxSysId("t_sys_dept_room",null ));
    }

    @Override
    protected BaseDao<SysDeptRoom, String> getDao() {
        return dao;
    }

    public List<SysDeptRoomDataBean> queryDeptListToModel(SysDeptRoomFilter filter){
        List<SysDept> deptList = deptService.query();
        Map<String, SysDept> map = new HashMap<>();
        for (SysDept sysDept : deptList) {
            if(!map.containsKey(sysDept.getDeptId())){
                map.put(sysDept.getSysId(), sysDept);
            }
        }
        List<SysDeptRoom> list = dao.queryDeptList(filter);
        List<SysDeptRoomDataBean> result = new ArrayList<>();
        for (SysDeptRoom sysDeptRoom : list) {
            SysDeptRoomDataBean dataBean = new SysDeptRoomDataBean();
            SysDept dept = map.get(sysDeptRoom.getDeptId());
            if(ObjectUtils.isNotBlank(dept)){
                dataBean.setDeptName(dept.getDeptName());
            }
            BeanUtils.copyProperties(sysDeptRoom, dataBean);
            if(ObjectUtils.isNotBlank(sysDeptRoom.getFgLeader())){
                SysLogin user = loginService.getById(sysDeptRoom.getFgLeader());
                dataBean.setFgLeader(user.getSysId());
                dataBean.setFgLeaderName(user.getUsername());
            }
            result.add(dataBean);
        }
        return result;
    }

    public List<SysDeptRoom> getListRoomById(String deptId){
        SysDeptRoomFilter filter = new SysDeptRoomFilter();
        filter.setDeptId(deptId);
        return dao.queryDeptList(filter);
    }




    public void createDeptRoom(SysDeptRoomFilter filter){
        SysDeptRoom targetBean = new SysDeptRoom();
        if(StringUtils.isBlank(filter.getOrderBy())){
            filter.setOrderBy("1");
        }
        BeanUtils.copyProperties(filter, targetBean);
        this.save(targetBean);
        targetBean.setRoomId(targetBean.getSysId());
        this.update(targetBean);
    }

    public List<SysDeptRoom> getByLeader(String leadid)
    {
        SysDeptRoomFilter filter = new SysDeptRoomFilter();
        filter.setFgLeader(leadid);
        return dao.queryDeptList(filter);

    }


    private List<DeptRoomDTO> deptEntityToDto(List<SysDept> listDepts){
//        List<SysDept> listDepts = deptService.query();
        List<DeptRoomDTO> listDept= new ArrayList<>();

        for (SysDept deptItem : listDepts) {
            DeptRoomDTO dept = new DeptRoomDTO();
            dept.setSysId(deptItem.getSysId());
            dept.setName(deptItem.getDeptName());
            dept.setState(deptItem.getState());
            dept.setFgLeader("");
            dept.setOrderBy(deptItem.getOrderBy()+"");
            dept.setTree(1);

            SysDeptRoomFilter queryFilter = new SysDeptRoomFilter();
            queryFilter.setDeptId(dept.getSysId());
            List<SysDeptRoom> sysDeptRooms = dao.queryDeptList(queryFilter);
            List<DeptRoomDTO> listRoom= new ArrayList<>();

            for (SysDeptRoom roomItem : sysDeptRooms) {
                DeptRoomDTO room = new DeptRoomDTO();
                room.setName(roomItem.getRoomName());
                room.setSysId(roomItem.getSysId());
                room.setState(roomItem.getState());
                room.setFgLeader(roomItem.getFgLeader());
                room.setOrderBy(roomItem.getOrderBy());
                room.setTree(2);
                listRoom.add(room);
            }
            dept.setChild(listRoom);
            listDept.add(dept);
        }
        return listDept;
    }


    public List<DeptRoomDTO> getTreeDeptRoom(SysDeptRoomFilter filter)
    {
//        List<DeptRoomDTO> listDept= new ArrayList<>();
        List<SysDept> allDepts = deptService.query();
        String userId = SysUserUtils.getUserId();
        List<DeptRoomDTO> listDept=new ArrayList<>();
        if("admin".equals(userId)){

         listDept= deptEntityToDto(allDepts);
        }else {
            String deptId = SysUserUtils.getDeptId();
            allDepts= allDepts.stream().filter(sysDept -> sysDept.getSysId().equals(deptId)).collect(Collectors.toList());
            listDept=deptEntityToDto(allDepts);
        }

        return listDept;

    }


    public List<DeptRoomDTO> getByKeyWords(String keyWords)
    {
        List<SysDept> depts= new ArrayList<>();
        SysDeptFilter filter = new SysDeptFilter();
        filter.setDeptName(keyWords);
        List<SysDept> query = deptService.query(filter);

        SysDeptRoomFilter roomfilter = new SysDeptRoomFilter();
        roomfilter.setRoomName(keyWords);
        if(!"admin".equals(SysUserUtils.getUserId()))
        {
            String deptId = SysUserUtils.getDeptId();
            query = query.stream().filter(sysDept -> sysDept.getSysId().equals(deptId)).collect(Collectors.toList());
            roomfilter.setDeptId(deptId);
        }
        List<SysDeptRoom> sysDeptRooms = this.dao.queryDeptList(roomfilter);
        if(ObjectUtils.isNotBlank(sysDeptRooms))
        {

            Map<String, String> collect = sysDeptRooms.stream().collect(Collectors.toMap(SysDeptRoom::getDeptId, SysDeptRoom::getSysId));
            HashMap<String,String> idsMap = new HashMap<>();
            for (SysDept dept : query) {
                if(!collect.containsKey(dept.getSysId())){
                    String s = collect.get(dept.getSysId());
                    if(StringUtils.isNotBlank(s))
                    {
                        idsMap.put(dept.getSysId(),collect.get(dept.getSysId()));
                    }
                }
            }


            for(Map.Entry<String,String> entry:idsMap.entrySet() ){
                String deptSysId = entry.getValue();
                SysDept entity = deptService.getById(deptSysId);
                depts.add(entity);
            }
        }
        depts.addAll(query);

        List<DeptRoomDTO> deptRoomDTOS = deptEntityToDto(depts);
        return deptRoomDTOS;

    }

}
